#include "stdafx.h"
#include <QEnvString.h>

size_t String::npos = std::string::npos;

bool operator == ( const std::string &a, const String &b ){ return a == b.d; }
bool operator != ( const std::string &a, const String &b ){ return a != b.d; }

std::ostream& operator << ( std::ostream&s, const String &b ){ s<<b.d; return s; }

String operator + ( const std::string&a, const String &b ){ return String(a) + b; }
String operator + ( const char *a, const String &b ){ return String(a) + b; }

String::String(void){}
String::String( const char* str ) : d(str){}
String::~String(void){}

String& String::operator += ( const char*rk ){d+=std::string(rk); return *this;}
String& String::operator += ( const QString &rk ){ d += FROMQT(rk); return *this; }
String& String::operator += ( const String &rk ){ d += rk.d; return *this; }
String& String::operator += ( const std::string &rk ){ d+= rk;  return *this; }
	
String String::operator + ( const char*rk ) const{ return String( d + std::string(rk) ); }
String String::operator + ( const String &rk ) const{return String( d + rk.d ); }
String String::operator + ( const QString &rk ) const{return String( d + FROMQT(rk).d ); }
String String::operator + ( const std::string &rk ) const{ return String( d + rk ); }
	
String& String::operator = ( const QString &rk ){ d = FROMQT(rk); return *this; }
String& String::operator = ( const String &rk ){ d = rk.d;  return *this;}
String& String::operator = ( const std::string &rk ){ d = rk;  return *this;}
String& String::operator = ( const char*rk ){ d = std::string(rk); return *this; }
	

 bool String::operator == ( const String &rk ){ return d == rk.d; }
bool String::operator == ( const std::string &rk ){ return d == rk; }

bool String::operator != ( const String &rk ){ return d != rk.d; }
bool String::operator != ( const std::string &rk ){ return d != rk; }
	
void String::clear(){ str(std::string()); }
bool String::empty() const{ return d.empty(); }
size_t String::size() const{ return d.size(); }

const std::string& String::data() const{ return d; }
std::string& String::data(){ return d; }

std::string String::str(const std::string& str){ d = str; return d; }

void String::insert( const size_t pos, const String &str_ )
{
	d.insert( pos, str_.cstr(), str_.size() );
}
bool String::replace( const String & what, const String& by )
{
	size_t p = 0;
	bool ret = false;
	while( (p=find( what, p )) != std::string::npos )
	{
		ret = true;
		remove( p, what.size() );
		insert( p, by );
	}

	return ret;
}
	
const char* String::cstr() const{ return d.c_str();  }

char String::at( const size_t i ) const{ return d.at(i); }
bool String::found( const String &sub, size_t fromPos ) const
{
	if( d.find( sub.d, fromPos ) == std::string::npos ) return false;
	return true;
}
	
void String::remove( const size_t id, const size_t count )
{
	if( id >= size() )return;
	d.erase( id, count );
}
bool String::operator == ( const QString &rk ){ return d == String(rk).d; }
bool String::operator == ( const char* rk ){ return d == std::string(rk); }
bool String::operator != ( const QString &rk ){ return d != String(rk); }
bool String::operator != ( const char* rk ){ return d != std::string(rk); }
String String::substr( const size_t pos, const size_t count ) const
{
	return d.substr( pos, count );
}
String String::trimmed() const{
	String str(*this);
	while( !str.empty() && str.at(0) == ' ' ) str.remove( 0 );
	return str;
}

StringVector String::split( const String &token )
{
	return StringVector::split( *this, token );
}